﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PublicCom;
using System.Text.RegularExpressions;
using System.Data;

namespace UpdateApp
{
    /// <summary>
    /// 模板辅助类
    /// </summary>
    public class TempHelper
    {
        /// <summary>
        /// Dictionary 转 DataTable
        /// 按key为列生成一行DataRow
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private DataTable Convert(Dictionary<string, object> dic, DataTable dt = null)
        {
            if (dic == null || dic.Keys.Count == 0) return null;

            if (dt == null)
            {
                dt = new DataTable();

                foreach (var key in dic.Keys)
                    dt.Columns.Add(key, typeof(string));
            }

            DataRow dr = dt.NewRow();

            foreach (var item in dic.Keys)
                dr[item] = dic[item];

            dt.Rows.Add(dr);

            return dt;
        }

        /// <summary>
        /// Dictionary 转 DataTable
        /// 按key为列生成一行DataRow
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private DataTable Convert(Dictionary<string, string> dic, DataTable dt = null)
        {
            if (dic == null || dic.Keys.Count == 0) return null;

            if (dt == null)
            {
                dt = new DataTable();

                foreach (var key in dic.Keys)
                    dt.Columns.Add(key, typeof(string));
            }

            DataRow dr = dt.NewRow();

            foreach (var item in dic.Keys)
                dr[item] = dic[item];

            dt.Rows.Add(dr);

            return dt;
        }

        /// <summary>
        /// Dictionary集合转DataTable
        /// 将Dictionary的每个Key的值转换为DataTable的列名
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DataTable Convert(List<Dictionary<string, object>> list)
        {
            if (list == null || list.Count == 0) return null;

            DataTable dt = null;

            list.ForEach(i => dt = Convert(i, dt));

            return dt;
        }

        /// <summary>
        /// Dictionary集合转DataTable
        /// 将Dictionary的每个Key的值转换为DataTable的列名
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public DataTable Convert(List<Dictionary<string, string>> list)
        {
            if (list == null || list.Count == 0) return null;

            DataTable dt = null;

            list.ForEach(i => dt = Convert(i, dt));

            return dt;
        }

        public DataTable Convert(List<string> list)
        {
            if (list == null || list.Count == 0) return null;

            DataTable dt = new DataTable();
            dt.Columns.Add("index");
            dt.Columns.Add("value");

            for (int i = 0; i < list.Count; i++)
            {
                DataRow dr = dt.NewRow();

                dr[0] = i;
                dr[1] = list[i];

                dt.Rows.Add(dr);
            }

            return dt;
        }

        public DataTable Convert(Dictionary<string, string> list)
        {
            if (list == null || list.Count == 0) return null;

            DataTable dt = new DataTable();
            dt.Columns.Add("index");
            dt.Columns.Add("key");
            dt.Columns.Add("value");

            int i = 0;
            foreach (var key in list.Keys)
            {
                DataRow dr = dt.NewRow();

                dr[0] = i;
                dr[1] = key;
                dr[2] = list[key];

                dt.Rows.Add(dr);
                i++;
            }

            return dt;
        }

        /// <summary>
        /// 将模板文件缓存成新文件并读取
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string ReadFile(ReadFile obj, bool isCache = true, string temp = "Template")
        {
            if (isCache)
            {
                obj.SaveAsHtml("/" + temp + "/Cache/" + obj.GetFileName(""));

                return obj.ReadHtml("/" + temp + "/Cache/" + obj.GetFileName(""));
            }
            else
            {
                return obj.ReadHtml("/" + temp + "/" + obj.GetFileName(""));
            }
        }

        //ConvertSpace=0	保留字符间的空格 按|分隔每行
        //ConvertSpace=1	取消字符串间的空格并首字母大写
        //ConvertSpace=2	取消字符串间的空格并用_分隔
        //ConvertSpace=3	不做转换
        //ConvertSpace=其它	取消字符串间的空格
        //忽略//开头的行
        public string ReadFile(string filePath, int ConvertSpace)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                FileStream file = null;
                StreamReader read = null;

                if (File.Exists(filePath))
                {
                    file = new FileStream(filePath, FileMode.Open);

                    read = new StreamReader(file, Encoding.UTF8);

                    string str = read.ReadLine();

                    while (str != null)
                    {
                        if (ConvertSpace == 1)
                        {
                            string flag = "";
                            if (!str.StartsWith("//"))
                                foreach (string i in str.Split(' '))
                                    if (i.Trim().Length > 0)
                                        flag += i.Substring(0, 1).ToUpper() + i.Substring(1);

                            sb.Append(" " + flag);
                        }
                        else if (ConvertSpace == 0)
                        {
                            if (!str.StartsWith("//"))
                                sb.Append("|" + str);
                        }
                        else if (ConvertSpace == 2)
                        {
                            string flag = "";
                            if (!str.StartsWith("//"))
                                foreach (string i in str.Split(' '))
                                    if (i.Trim().Length > 0)
                                        flag += "_" + i.Substring(0, 1).ToUpper() + i.Substring(1);

                            sb.Append(" " + flag.Substring(1, flag.Length - 1));
                        }
                        else if (ConvertSpace == 3)
                        {
                            if (str != "")
                                sb.Append(str);
                        }
                        else
                        {
                            str = Regex.Replace(str, @"\s", "");

                            if (!str.StartsWith("//"))
                                sb.Append(" " + str);
                        }

                        str = read.ReadLine();
                    }

                    file.Dispose();
                    read.Dispose();
                }
            }
            catch (Exception)
            {
            }

            return sb.ToString();
        }
    }
}